using System;
using FireStarter.Base;
using Habanero.BO;
using Habanero.BO.ClassDefinition;
using Habanero.BO.Loaders;
using Habanero.DB;
using NUnit.Framework;

namespace Firestarter.DB.Schema.Test.Logic
{
    public class TestBase
    {
        [TestFixtureSetUp]
        public virtual void TestFixtureSetup()
        {
            ClassDef.ClassDefs.Clear();
            string xmlClassDefs = BOBroker.GetClassDefsXml();
            ClassDef.ClassDefs.Add(new XmlClassDefsLoader(xmlClassDefs, new DtdLoader()).LoadClassDefs());

//            DatabaseConfig databaseConfig = new DatabaseConfig("Mysql", "localhost", _databaseName, "root", "root", "3306");
//            IDatabaseConnection databaseConnection = databaseConfig.GetDatabaseConnection();
//            string sqlStatement = string.Format(@"
//                DROP DATABASE IF EXISTS {0};
//                CREATE DATABASE IF NOT EXISTS {0};
//                USE {0};
//                ", _databaseName);
//            databaseConnection.ExecuteRawSql(sqlStatement);
//            DatabaseConfig newConfig = CloneDatabaseConfig(databaseConfig, null, null, _databaseName, null, null, null);
//            _databaseConnection = newConfig.GetDatabaseConnection();
//            if (DatabaseConnection.CurrentConnection == null)
//            {
//                //DatabaseConnection.CurrentConnection = databaseConfig.GetDatabaseConnection();
//                //BORegistry.DataAccessor = new DataAccessorDB();
            BORegistry.DataAccessor = new DataAccessorInMemory();
            //}
        }

        [SetUp]
        public virtual void SetupTest()
        {
//            DatabaseConfig databaseConfig = new DatabaseConfig("Mysql", "localhost", _databaseName, "root", "root", "3306");
//            IDatabaseConnection databaseConnection = databaseConfig.GetDatabaseConnection();
//            string sqlStatement = string.Format(@"
//                DROP DATABASE IF EXISTS {0};
//                CREATE DATABASE IF NOT EXISTS {0};
//                USE {0};
//                ", _databaseName);
//            databaseConnection.ExecuteRawSql(sqlStatement);
//            DatabaseConfig newConfig = CloneDatabaseConfig(databaseConfig, null, null, _databaseName, null, null, null);
//            _databaseConnection = newConfig.GetDatabaseConnection();
//            if (DatabaseConnection.CurrentConnection == null)
//            {
//                //DatabaseConnection.CurrentConnection = databaseConfig.GetDatabaseConnection();
//                //BORegistry.DataAccessor = new DataAccessorDB();
//                BORegistry.DataAccessor = new DataAccessorInMemory();
//            }
        }

        public static DatabaseConfig CloneDatabaseConfig
            (DatabaseConfig databaseConfig, string newVendor, string newServer, string newDatabase, string newUserName,
             string newPassword, string newPort)
        {
            string vendor = databaseConfig.Vendor;
            string server = databaseConfig.Server;
            string databaseName = databaseConfig.Database;
            string userName = databaseConfig.UserName;
            string password = databaseConfig.Password;
            string port = databaseConfig.Port;
            if (!String.IsNullOrEmpty(newVendor))
            {
                vendor = newVendor;
            }
            if (!String.IsNullOrEmpty(newServer))
            {
                server = newServer;
            }
            if (!String.IsNullOrEmpty(newDatabase))
            {
                databaseName = newDatabase;
            }
            if (!String.IsNullOrEmpty(newUserName))
            {
                userName = newUserName;
            }
            if (!String.IsNullOrEmpty(newPassword))
            {
                password = newPassword;
            }
            if (!String.IsNullOrEmpty(newPort))
            {
                port = newPort;
            }
            DatabaseConfig newDatabaseConfig = new DatabaseConfig
                (vendor, server, databaseName, userName, password, port);
            return newDatabaseConfig;
        }
    }
}